package com.house.controller.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.house.bean.*;
import com.house.service.HDishService;
import com.house.service.HOrdersService;
import com.house.service.HUserService;
import com.house.utils.ChangeStatus;
import com.house.utils.ChangeTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//订单控制层
@RestController
public class OrderController {
    @Autowired
    private HOrdersService hOrdersService;
    @Autowired
    private HUserService hUserService;
    @Autowired
    private HDishService hDishService;
    /**
     * 根据用户id 查看用户待付款订单 status:1 表示待付款
     */
    @RequestMapping(value = "/order/selectOrderByStatus" , method = RequestMethod.GET)
    public Object selectOrderByStatus(@RequestParam(value = "pn",defaultValue = "1") Integer pn,@RequestParam ("uid") int uid,@RequestParam ("status") int status) throws ParseException {
        JSONObject jsonObject = new JSONObject();
        //分页查询数据
        Page<HOrders> hOrdersPage = new Page<>(pn, 5);
        //分页查询结果
        Page<HOrders> page = hOrdersService.page(hOrdersPage,new QueryWrapper<HOrders>().eq("uid",uid).eq("status",status));
        List<HOrders> list = page.getRecords();
        List<OrdersUser> ordersUsers = new ArrayList<>();
        for (int i = 0; i<list.size(); i++) {
            HUser hUser = hUserService.getOne(new QueryWrapper<HUser>().eq("mid",list.get(i).getMid()));//根据师傅编号查询用户表
            OrdersUser ordersUser = new OrdersUser();
            ordersUser.setId(list.get(i).getId());//订单编号
            ordersUser.setName(hUser.getName());//师傅名
            ordersUser.setBookingTime(list.get(i).getBookingTime());//预约时间
            ordersUser.setAddress(list.get(i).getAddress()); //地址
            ordersUser.setMoney(list.get(i).getMoney());
            ordersUser.setStatus(ChangeStatus.changOfString(list.get(i).getStatus()));//状态
            String str = list.get(i).getCid();
            String[] cids=str.split("-");//通过- 划分菜单编号
            String food = "";
            for(int k = 0;k<cids.length; k++){
                HDish dish = hDishService.getOne(new QueryWrapper<HDish>().eq("id", Integer.parseInt(cids[k])));//通过菜单编号去查找dish表获取菜名
                food += dish.getName()+"   ";
            }
            ordersUser.setDishName(food);
            ordersUsers.add(ordersUser);
        }
        jsonObject.put("records",ordersUsers);
        jsonObject.put("size",page.getSize());
        jsonObject.put("total",page.getTotal());
        return jsonObject;
    }
}
